# 实现WSGI协议中的application接口方法
import re


def application(envrion,start_response):
    """
    envrion-->字典，保存访问路径
    start_response -->用来回调，实现将web应用的处理结构回传给调用者
    返回值-->响应体，根据访问路径得到相应的值
    """
    url_path = envrion['PATH_INFO']
    #确定路由
    router_dict = {'/index.py':index,'/center.py':center}
    file_content = other()
    if url_path in router_dict:
        file_content = router_dict[url_path]()


    # 回调start_response函数，将响应状态信息回传给服务器
    start_response('200 OK',[('Content-Type','text/html;charset=utf-8')])
    #返回响应的数据内容
    return file_content


def other():
    file_content = 'other page ...'
    return file_content


def center():
    path = './templates/center.html'
    with open(path, 'r') as f:
        file_content = f.read()
    row_str = """
                <tr>
                    <td>000426</td>
                    <td>兴业矿业</td>
                    <td>0.41%</td>
                    <td>2.17%</td>
                    <td>9.71</td>
                    <td>9.67</td>
                    <td>今天的涨幅不错,希望每天如此</td>
                    <td>
                        <a type="button" class="btn btn-default btn-xs" href="/update/000426.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
                    </td>
                    <td>
                        <input type="button" value="删除" id="toDel" name="toDel" systemidvaule="000426">
                    </td>
                </tr> """
    all_data = ''
    for _ in range(5):
        all_data += row_str
    file_content = re.sub(r'\{%content%\}', all_data, file_content)
    return file_content


def index():
    path = './templates/index.html'
    with open(path, 'r') as f:
        file_content = f.read()
    row_str = """
                    <tr>
                        <td>1</td>
                        <td>000007</td>
                        <td>全新好</td>
                        <td>10.01%</td>
                        <td>4.40%</td>
                        <td>16.05</td>
                        <td>14.60</td>
                        <td>2017-07-18</td>
                        <td>
                            <input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007">
                        </td>
                    </tr>  """
    all_data = ''
    for i in range(20):
        all_data += row_str
    file_content = re.sub(r'\{%content%\}', all_data, file_content)
    return file_content




